
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>商品列表</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="goods_id" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.rlgood.insert">
                <i class="layui-icon layui-icon-add-1"></i>新增
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.rlgood.delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            {{# if(d.is_enroll_period==1){}}
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="period" permission="app.admin.rlgood.period">营期</button>
            {{# } }}
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="specification" permission="app.admin.rlgood.specification">规格</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.rlgood.update">编辑</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.rlgood.delete">删除</button>
        </script>

        <script type="text/html" id="createTime">
            {{# if(d.create_time>0){}}
                {{layui.util.toDateString(d.create_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script type="text/html" id="goodsEndTime">
            {{# if(d.is_deadline==1){}}
            {{layui.util.toDateString(d.goods_end_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# }else{ }}
            不限制
            {{# } }}
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/rl-goods/select";
            const UPDATE_API = "/app/admin/rl-goods/update";
            const DELETE_API = "/app/admin/rl-goods/delete";
            const INSERT_URL = "/app/admin/rl-goods/insert";
            const UPDATE_URL = "/app/admin/rl-goods/update";
            const SPEC_SELECT_API = "/app/admin/rl-goods/specification";
            const PERIOD_SELECT_API="/app/admin/rl-goods/period"
            const UPDATE_STATUS_URL ='/app/admin/rl-goods/updateStatus'
            // 字段 创建时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                });
            })
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						title: "商品ID",align: "left",
						field: "id",
                        width: '80',
					},{
						title: "商品编号",align: "left",
						field: "goods_sn",
                        width:'120',
					},{
                        title: "商品标题",align: "left",
                        field: "goods_title",
                        width: '150',
                    },{
						title: "商品图片",align: "left",
						field: "goods_icon",
						templet: function (d) {
							return '<img src="'+encodeURI(d['goods_icon'])+'" style="max-width:32px;max-height:32px;" alt="" />'
						},
                        width: '100',
					},{
                        title: "推荐理由",align: "left",
                        field: "goods_title",
                        width: '150',
                    },{
                        title: "商品分类",align: "left",
                        field: "goods_cate_title",
                        width: '150',
                    },{
                        title: "商品总销量",align: "left",
                        field: "goods_sale_num",
                        width: '150',
                    },{
                        title: "商品总库存",align: "left",
                        field: "goods_stock_num",
                        width: '150',
                    },{
                        title: "商品地点",align: "left",
                        field: "goods_location",
                        width: '180',
                    },{
                        title: "集合地点",align: "left",
                        field: "address_details",
                        width: '180',
                    },{
                        title: "商品周期",align: "left",
                        field: "goods_activity_cycle",
                        width: '150',
                    },{
                        title: "商品标签",align: "left",
                        field: "goods_labels",
                        width: '150',
                    },{
						title: "首单分账模式",align: "left",
						field: "is_allocation_mode",
                        width: '150',
                        templet: function (d) {
                            let css = {"0":"layui-bg-warn","1":"layui-bg-green","2":"layui-bg-blue"}[d["is_allocation_mode"]];
                            let title={"0":"未启用","1":"比例分账","2":"固定金额分账"}[d["is_allocation_mode"]];
                            return '<span class="layui-badge '+css+'">'+util.escape(title)+'</span>';
                        }
					},{
                        title: "首单分账数值",align: "left",
                        field: "allocation_num",
                        width: '120',
                    },{
                        title: "复单分账模式",align: "left",
                        field: "is_repurchase_allocation_mode",
                        width: '150',
                        templet: function (d) {
                            let css = {"0":"layui-bg-warn","1":"layui-bg-green","2":"layui-bg-blue"}[d["is_repurchase_allocation_mode"]];
                            let title={"0":"未启用","1":"比例分账","2":"固定金额分账"}[d["is_repurchase_allocation_mode"]];
                            return '<span class="layui-badge '+css+'">'+util.escape(title)+'</span>';
                        }
                    },{
                        title: "复单分账数值",align: "left",
                        field: "repurchase_allocation_num",
                        width: '120',
                    },{
                        title: "推荐状态",align: "left",
                        field: "is_push",
                        width: '100',
                        templet: function (d) {
                            let field = "is_push";
                            form.on("switch("+field+")", function (data) {
                                let load = layer.load();
                                let postData = {};
                                postData[field] = data.elem.checked ? 1 : 2;
                                postData[PRIMARY_KEY] = this.value;
                                $.post(UPDATE_STATUS_URL, postData, function (res) {
                                    layer.close(load);
                                    if (res.code) {
                                        return layui.popup.failure(res.msg, function () {
                                            data.elem.checked = !data.elem.checked;
                                            form.render();
                                        });
                                    }
                                    return layui.popup.success("操作成功");
                                })
                            });
                            let checked = d[field] === 1 ? "checked" : "";
                            return '<input type="checkbox" value="'+util.escape(d[PRIMARY_KEY])+'" lay-filter="'+util.escape(field)+'" lay-skin="switch" lay-text="'+util.escape('')+'" '+checked+'/>';
                        }
                    },{
                        title: "上架状态",align: "left",
                        field: "is_upper",
                        width: '100',
                        templet: function (d) {
                            let field = "is_upper";
                            form.on("switch("+field+")", function (data) {
                                let load = layer.load();
                                let postData = {};
                                postData[field] = data.elem.checked ? 1 : 2;
                                postData[PRIMARY_KEY] = this.value;

                                $.post(UPDATE_STATUS_URL, postData, function (res) {
                                    layer.close(load);
                                    if (res.code) {
                                        return layui.popup.failure(res.msg, function () {
                                            data.elem.checked = !data.elem.checked;
                                            form.render();
                                        });
                                    }
                                    return layui.popup.success("操作成功");
                                })
                            });
                            let checked = d[field] === 1 ? "checked" : "";
                            return '<input type="checkbox" value="'+util.escape(d[PRIMARY_KEY])+'" lay-filter="'+util.escape(field)+'" lay-skin="switch" lay-text="'+util.escape('')+'" '+checked+'/>';
                        }
                    },{
						title: "截止时间",align: "left",
						field: "goods_end_time",
                        templet: '#goodsEndTime',
                        width: '120',
					},{
                        title: "创建时间",align: "left",
                        field: "create_time",
                        templet: '#createTime',
                        width: '120',
                    },{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: '200',
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				// 获取表格中下拉或树形组件数据
				let apiResults = {};
				apiResults["is_enroll_period"] = [];
				apiResults["is_allocation_mode"] = [];
				apiResults["is_repurchase_allocation_mode"] = [];
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }else if(obj.event ==='specification'){
                        specification(obj)
                    }else if(obj.event ==='period'){
                        period(obj)
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"900px", common.isModile()?"100%":"600px"],
                        content: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"900px", common.isModile()?"100%":"600px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 表格规格数据
                let specification = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "规格列表",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"800px", common.isModile()?"100%":"450px"],
                        content: SPEC_SELECT_API + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 表格规格数据
                let period = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "营期列表",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"800px", common.isModile()?"100%":"450px"],
                        content: PERIOD_SELECT_API + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

        </script>
    </body>
</html>
